//
// Character.cpp for  in /home/gomes_m//RPBaraze
// 
// Made by mickael gomes
// Login   <gomes_m@epitech.net>
// 
// Started on  Fri Sep  6 14:14:24 2013 mickael gomes
// Last update Fri Sep  6 17:18:49 2013 mickael gomes
//

#include	       <cstdlib>
#include	       "Ebouli.hh"
#include	       "CoupDeCoutal.hh"
#include	       "Character.hh"

Character::Character(std::string const &name)
  : hp_(100), name_(name)
{
  attack_.push_back(new CoupDeCoutal);
  attack_.push_back(new Ebouli);
}

Character::~Character()
{
}

void			Character::takeDmg(int dmg)
{
  hp_ -= dmg;
  if (hp_ < 0)
    hp_ = 0;
  std::cout << name_ << " => HP: " << hp_ << "/100" << std::endl; 
}

bool			Character::isAlive()
{
  if (hp_ > 0)
    return (true);
  return (false);
}

void			Character::receiveHeal(int heal)
{

}

int			Character::getKey()
{
  int			n;
  std::string		k;
  std::string		message;
  std::string		pos;
  char			*tmp;

  std::cout << this->name_ << "'s round" << std::endl;
  do
    {
      message = "Attacks : ";
      for (unsigned int i = 0; i < attack_.size(); i++)
	{
	  pos = (i + 1) + '0';
	  message += attack_[i]->getName() + " (" + pos +  ")";
	  if (i != attack_.size() - 1)
	    message += ", ";
	  else
	    message += ".";
	}
      std::cout << message << std::endl;
      std::cin >> k;
      n = atoi(k.c_str());
    } while (n > attack_.size() || n <= 0);
  return (n - 1);
}

void			Character::attack(Character &other)
{
  int			key = getKey();
  int			dmg = attack_[key]->doDamage();

  std::cout << name_ << " attack " << other.getName() << " for " << dmg << " with " << attack_[key]->getName();
  if (attack_[key]->hasCrit())
    std::cout << " (CRITICAL HIT !!!)" << std::endl;
  else
    std::cout << "." << std::endl;
  other.takeDmg(dmg);
  std::cout << "-------------------------------" << std::endl;
}

int			Character::getLife() const
{
  return (life_);
}

int			Character::getHp() const
{
  return (hp_);
}

std::string	const	&Character::getName() const
{
  return (name_);
}
